package com.bank.study;

public class ContextSwitchDemo {
    private static final long count = 1000000;
    public static void main(String[] args) throws InterruptedException {
        concurrency();
        serial();
    }

    private static void serial() throws InterruptedException {
        long start = System.currentTimeMillis();
        int a = 0;
        for (int i = 0; i < count; i++) {
            a ++;
        }
        int b = 0;
        for (int i = 0; i < count; i++) {
            b ++;
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("serial: " + time + "ms,b: "+ b );
    }

    private static void concurrency() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread thread = new Thread(()->{
            int a = 0;
            for (int i = 0; i < count; i++) {
                a ++;
                System.out.println("a: "+ a );
            }

        });
        thread.start();
        int b = 0;
        for (int i = 0; i < count; i++) {
            b ++;
            System.out.println("b: "+ b );
        }
        thread.join();
        long time = System.currentTimeMillis() - start;
        System.out.println("concurrency: " + time + "ms,b: "+ b );
    }
}
